#include <stdio.h>
#include <stdlib.h>
#include "Structs.h"
#include "Pong3D.h"
#include <GL/glut.h>
#include <math.h>
#include <Windows.h>


void drawSkybox(void){
	GLfloat scale = 5.0f;
	int i = 0;
	glPushMatrix();
	
	// Reset and transform the matrix.
	 glLoadIdentity();
	//Enable/Disable features
	glPushAttrib(GL_ENABLE_BIT);
	glEnable(GL_TEXTURE_2D);
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);
	_debugRotate += 0.1f;
	glRotatef(_debugRotate,0,1,0);
	// Just in case we set all vertices to white.
	glColor4f(1,1,1,1);

	// Render the front quad
	glBindTexture(GL_TEXTURE_2D, _skyBox[0]);
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex4f(  0.5f*scale, -0.5f*scale, -0.5f*scale, 1.0f );
	glTexCoord2f(1, 0); glVertex4f( -0.5f*scale, -0.5f*scale, -0.5f*scale, 1.0f );
	glTexCoord2f(1, 1); glVertex4f( -0.5f*scale,  0.5f*scale, -0.5f*scale, 1.0f );
	glTexCoord2f(0, 1); glVertex4f(  0.5f*scale,  0.5f*scale, -0.5f*scale, 1.0f );
	glEnd();


	// Render the left quad
	glBindTexture(GL_TEXTURE_2D, _skyBox[1]);
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex4f(  0.5f*scale, -0.5f*scale,  0.5f*scale, 1.0f);
	glTexCoord2f(1, 0); glVertex4f(  0.5f*scale, -0.5f*scale, -0.5f*scale, 1.0f);
	glTexCoord2f(1, 1); glVertex4f(  0.5f*scale,  0.5f*scale, -0.5f*scale, 1.0f);
	glTexCoord2f(0, 1); glVertex4f(  0.5f*scale,  0.5f*scale,  0.5f*scale, 1.0f);
	glEnd();

	// Render the back quad
	glBindTexture(GL_TEXTURE_2D, _skyBox[2]);
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex4f( -0.5f*scale, -0.5f*scale,  0.5f*scale, 1.0f );
	glTexCoord2f(1, 0); glVertex4f(  0.5f*scale, -0.5f*scale,  0.5f*scale, 1.0f );
	glTexCoord2f(1, 1); glVertex4f(  0.5f*scale,  0.5f*scale,  0.5f*scale, 1.0f );
	glTexCoord2f(0, 1); glVertex4f( -0.5f*scale,  0.5f*scale,  0.5f*scale, 1.0f );

	glEnd();

	// Render the right quad
	glBindTexture(GL_TEXTURE_2D, _skyBox[3]);
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex4f( -0.5f*scale, -0.5f*scale, -0.5f*scale, 1.0f );
	glTexCoord2f(1, 0); glVertex4f( -0.5f*scale, -0.5f*scale,  0.5f*scale, 1.0f );
	glTexCoord2f(1, 1); glVertex4f( -0.5f*scale,  0.5f*scale,  0.5f*scale, 1.0f );
	glTexCoord2f(0, 1); glVertex4f( -0.5f*scale,  0.5f*scale, -0.5f*scale, 1.0f );
	glEnd();

	// Render the top quad
	glBindTexture(GL_TEXTURE_2D, _skyBox[4]);
	glBegin(GL_QUADS);
	glTexCoord2f(0, 1); glVertex4f( -0.5f*scale,  0.5f*scale, -0.5f*scale, 1.0f );
	glTexCoord2f(0, 0); glVertex4f( -0.5f*scale,  0.5f*scale,  0.5f*scale, 1.0f );
	glTexCoord2f(1, 0); glVertex4f(  0.5f*scale,  0.5f*scale,  0.5f*scale, 1.0f );
	glTexCoord2f(1, 1); glVertex4f(  0.5f*scale,  0.5f*scale, -0.5f*scale, 1.0f );
	glEnd();

	// Render the bottom quad
	glBindTexture(GL_TEXTURE_2D, _skyBox[5]);
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex4f( -0.5f*scale, -0.5f*scale, -0.5f*scale, 0.0f );
	glTexCoord2f(0, 1); glVertex4f( -0.5f*scale, -0.5f*scale,  0.5f*scale, 0.0f );
	glTexCoord2f(1, 1); glVertex4f(  0.5f*scale, -0.5f*scale,  0.5f*scale, 0.0f );
	glTexCoord2f(1, 0); glVertex4f(  0.5f*scale, -0.5f*scale, -0.5f*scale, 0.0f );
	glEnd();

	// Restore enable bits and matrix
	glPopAttrib();
	glPopMatrix();
}

void loadSkyBox(void){
	int i;
	if(__debug){
		//raw_texture_load("skybox\\1.tga",1024,1024);
	_skyBox[0] = raw_texture_load("skybox\\1.tga",1024,1024); //TOP
	printf("1 Loaded");
	_skyBox[1] = raw_texture_load("skybox\\2.tga", 1024, 1024); //left
	printf("2 Loaded");
	_skyBox[2] = raw_texture_load("skybox\\3.tga", 1024, 1024);//right
	printf("3 Loaded");
	_skyBox[3] = raw_texture_load("skybox\\4.tga", 1024, 1024);//front
	printf("4 Loaded");
	_skyBox[4] = raw_texture_load("skybox\\5.tga", 1024, 1024);//back 
	printf("5 Loaded");
	_skyBox[5] = raw_texture_load("skybox\\6.tga", 1024, 1024);
	printf("6 Loaded");
	raw_texture_load("skybox\\1.tga",1024,1024);
	//raw_texture_load("skybox\\6.tga",1024,1024);
	}else{
	
	_skyBox[0] = raw_texture_load("skybox\\ssky1.tga",1024,1024);
	_skyBox[1] = raw_texture_load("skybox\\ssky2.tga", 1024, 1024);
	_skyBox[2] = raw_texture_load("skybox\\ssky3.tga", 1024, 1024);
	_skyBox[3] = raw_texture_load("skybox\\ssky4.tga", 1024, 1024);
	_skyBox[4] = raw_texture_load("skybox\\ssky5.tga", 1024, 1024);
	_skyBox[5] = raw_texture_load("skybox\\ssky6.tga", 1024, 1024);
	raw_texture_load("skybox\\ssky1.tga", 1024, 1024);
	
	}

	_skyBox[5] = raw_texture_load("skybox\\ssky6.tga", 1024, 1024);

	for (i=0; i < 6; i ++){
		if (_skyBox[i] == 0){
			printf("FAIL ON %i",i);
		}
	}
}